package com.gxa.p2p.controller;

import com.gxa.p2p.common.JsonResult;
import com.gxa.p2p.common.exception.MyException;
import com.gxa.p2p.pojo.Agreement;
import com.gxa.p2p.service.AgreementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/agreement")
@WebAppConfiguration
//@Transactional
@Api(value = "协议接口")
public class AgreementController {
    @Autowired
    private AgreementService agreementService=null;
    /**
     * 罗雪锋
     * @param agreement
     * @return
     */
    @PostMapping("addAgreement")
    @ApiOperation("新增协议")
    public Map addAgreement( Agreement agreement) throws MyException {
        agreementService.add(agreement);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    /**
     * 罗雪锋
     * @param aid
     * @param astate
     * @return
     */
    @PostMapping("updateAgreementState")
    @ApiOperation("修改协议状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "aid",value = "协议id",required = true),
            @ApiImplicitParam(name = "astate",value = "协议状态",required = true)
    })
    public Map updateAgreementState(@RequestParam("aid") Long aid,@RequestParam("astate") Boolean astate) throws MyException {
        agreementService.update(aid, astate);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
    /**
     * 罗雪锋
     * @param page
     * @param size
     * @return
     */
    @PostMapping("findAgreementByPage")
    @ApiOperation("查询所有协议")
    @ApiImplicitParams({
            /*@ApiImplicitParam(name = "aid",value = "id",required = false),*/
            @ApiImplicitParam(name = "page",value = "页数",required = true),@ApiImplicitParam(name = "size",value = "每页数据量",required = false)})
    public Map findAgreementByPage(
            /*@RequestParam("aid" ) Long aid,*/
            @RequestParam("page") Integer page,@RequestParam("size") Integer size){
        List<Agreement> maps = agreementService.selecctAll(page, size);
        List<Agreement> agreements = agreementService.selectAllData();
        int size1 = agreements.size();
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.put("count",size1);
        return jsonResult.getDataMap();
    }
    @PostMapping("selectById")
    @ApiOperation("通过id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aid",value = "id",required = true)
    })
    public Map selectById(@RequestParam("aid") Long aid){
        Agreement agreement = agreementService.selectById(aid);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(agreement);
        return jsonResult.getDataMap();
    }

}
